<?php

/**
 * Products basket stored in session
 * @author shenlon
 */
class Basket {

    /**
     * Array of items in basket
     * @var BasketItem
     */
    private $items;

    /**
     * Default constructor, loading basket from session 
     */
    public function __construct() {
        $this->items = array();
        $this->loadBasket();
    }

    /**
     * Adds an item to basket
     * @param Product $item
     * @return boolean
     * @throws Exception 
     */
    public function addItem($item) {
        if ($item instanceof Product) {
            if (!array_key_exists($item->id, $this->items)) {
                $this->items[$item->id] = new BasketItem($item);
            }
            else
                $this->items[$item->id]->quantity++;
            return $this->saveBasket();
        }
        else
            throw new Exception("Item to add to basket should be a Product object, not " . gettype($item));
    }

    /**
     * Removes an item from basket
     * @param int $id
     * @return boolean 
     */
    public function removeItem($id) {
        unset($this->items[$id]);
        return $this->saveBasket();
    }

    /**
     * Sets the quantity of an item in the basket
     * @param int $id
     * @param int $quantity
     * @return boolean 
     */
    public function setItemQuantity($id, $quantity) {
        if (isset($this->items[$id])) {
            if ($quantity <= 0) {
                $this->removeItem($id);
            } else {
                $this->items[$id]->quantity = intval($quantity);
                $this->saveBasket();
            }
            return true;
        }
        return false;
    }

    /**
     * Saves basket in session
     * @return boolean 
     */
    public function saveBasket() {
        $_SESSION['basket'] = serialize($this->items);
        return true;
    }

    /**
     * Loads basket from session
     * @return boolean 
     */
    public function loadBasket() {
        if (isset($_SESSION['basket'])) {
            $this->items = unserialize($_SESSION['basket']);
            return true;
        }
    }

    /**
     * Removes all products from basket 
     */
    public function clear() {
        foreach ($this->items as $item)
            unset($this->items[$item->product->id]);
        $this->saveBasket();
    }

    /**
     * Displays basket products and their quantities
     * @return type 
     */
    public function displayBasket() {
        return $this->items;
    }

    /**
     * Displays basket status - number of products and their total price 
     */
    public function displayBasketStatus() {
        echo $this;
    }

    /**
     * Returns total cost of items in the basket
     * @return int
     */
    public function getBasketTotalCost() {
        $totalCost = 0;
        foreach ($this->items as $product) {
            $totalCost += $product->product->price * $product->quantity;
        }
        return $totalCost;
    }

    public function __toString() {
        $count = count($this->items);
        switch ($count) {
            case 0:
                return "Twój koszyk jest pusty";
            case 1:
                return "Twój koszyk zawiera 1 produkt o łącznej wartości " . $this->getBasketTotalCost() . " zł";
            case 2:
            case 3:
            case 4:
                return "Twój koszyk zawiera $count produkty o łącznej wartości " . $this->getBasketTotalCost() . " zł";
            default:
                return "Twój koszyk zawiera $count produktów o łącznej wartości " . $this->getBasketTotalCost() . " zł";
        }
    }

}

?>
